﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
using Orchard.Environment.Extensions;

namespace MediaGarden.Documents.Attachments
{
    [OrchardFeature("MediaGarden.Documents.Attachments")]
    public class Migrations : DataMigrationImpl
    {
        public int Create()
        {
            ContentDefinitionManager.AlterTypeDefinition("Attachment", cfg => cfg
                 .WithPart("CommonPart")
                 .WithPart("ConnectorPart",part=>part
                     .WithSetting("ConnectorTypePartSettings.AllowedContentLeft","Page,Product,BlogPost")
                     .WithSetting("ConnectorTypePartSettings.AllowedContentRight", "Video,Audio,Playlist,Image,Document,Binary")
                     .WithSetting("ConnectorTypePartSettings.InverseConnectorType", "AttachmentOf")
                     .WithSetting("ConnectorTypePartSettings.SocketDisplayName", "Attachments")
                     )
                 .WithSetting("Stereotype", "Content")
            );
            ContentDefinitionManager.AlterTypeDefinition("AttachmentOf", cfg => cfg
                 .WithPart("CommonPart")
                 .WithPart("ConnectorPart", part => part
                     .WithSetting("ConnectorTypePartSettings.AllowedContentRight", "Page,Product,BlogPost")
                     .WithSetting("ConnectorTypePartSettings.AllowedContentLeft", "Video,Audio,Playlist,Image,Document,Binary")
                     .WithSetting("ConnectorTypePartSettings.InverseConnectorType", "Attachment")
                     .WithSetting("ConnectorTypePartSettings.SocketDisplayName", "Attachments Of")
                     )
                 .WithSetting("Stereotype", "Content")
            );

            // Give sockets to any playable media types
            ContentDefinitionManager.AlterTypeDefinition("Video", cfg => cfg
                .WithPart("SocketsPart"));
            ContentDefinitionManager.AlterTypeDefinition("Audio", cfg => cfg
                .WithPart("SocketsPart"));
            ContentDefinitionManager.AlterTypeDefinition("Playlist", cfg => cfg
                .WithPart("SocketsPart"));
            ContentDefinitionManager.AlterTypeDefinition("Image", cfg => cfg
                .WithPart("SocketsPart"));
            ContentDefinitionManager.AlterTypeDefinition("Document", cfg => cfg
                .WithPart("SocketsPart"));
            return 1;
        }

    }
}